<?php
namespace Admin\Controller;
use Think\Image;

use Think\Page;

use Think\Controller;
use Common\Controller\AuthController;
class ScenicOrderController extends AuthController {
	public function index(){
		$order = D("ScenicOrder");
		$count = $order->relation(true)->count();
		$page = new Page($count,7);
		$page->setConfig('prev', '上页');
		$page->setConfig('next', '下页');
		$show = $page->show();
		$list = $order->relation(true)->limit($page->firstRow,$page->listRows)->select();
		$this->assign('list',$list);
		$this->assign('count',$count);
		$this->assign('page',$show);
		$this->display();
	}
	public function getByType(){
		$order = D("ScenicOrder");
		$where = array();
		if(isset($_GET['type'])){
			$where['type'] = $_GET['type'];
		}
		$count = $order->relation(true)->where($where)->count();
		$page = new Page($count,7);
		$page->setConfig('prev', '上页');
		$page->setConfig('next', '下页');
		$show = $page->show();
		$list = $order->relation(true)->where($where)->limit($page->firstRow,$page->listRows)->select();
		$this->assign('list',$list);
		$this->assign('count',$count);
		$this->assign('page',$show);
		$this->display('index');
	}
	public function add(){
		if(IS_POST){
			$file = $_FILES['image'];
			$uploadpath = "Public/Upload/Ticket/";//上传路径

			$util = A('Util');
			$util->createDir($uploadpath);//上传路径是否已经存在，不存在则创建

			$pinfo = pathinfo($file['name']);
			$ftype = $pinfo['extension'];//文件类型
			$name = basename($file['name'],".{$ftype}");

			$fileName = md5($name.time()).".".$ftype;//识别图
			$image = new Image();
			$image->open($file['tmp_name']);
			$image->thumb('100', '100')->save($uploadpath.$fileName);

			$data = $_POST;
			$order = D("ScenicOrder");
			$data["image"] = $uploadpath.$fileName;
			$data = $order->create($data);
			$result= $order->add($data);
			if($result){
				$this->success("添加成功","index");
			}else{
				$this->error("添加失败");
			}
		}else{
			$scenic = D('Scenic');
			$list = $scenic->select();
			$this->assign("list",$list);
			$this->display();
		}
	}
	public function check(){
		$where['id'] = $_GET['id'];
		$order = D('ScenicOrder');
		$result = $order->where($where)->setField('type','2');
		if($result){
			$this->success("该订单已改为已消费！","index");
		}else{
			$this->error("操作失败！");
		}
	
	}
	public function delete(){
		$where['id'] = $_GET['id'];
		$order = D('ScenicOrder');
		$result = $order->where($where)->delete();
		if($result){
			$this->success("删除成功","index");
		}else{
			$this->error("删除失败");
		}
	}
}